#!/usr/bin/env ruby

class String
  def indent(n)
    prefix = " " * n
    self.lines.map { |line| prefix + line }.join
  end
end

base_images = {
  nil => "buildpack-deps:stable",
  "slim" => "debian:stable-slim",
}

envs = <<~EOF
  ENV LANG=C.UTF-8
  # don't create ".bundle" in all our apps
  ENV GEM_HOME=/usr/local/bundle
  ENV BUNDLE_SILENCE_ROOT_WARNING=1 \\
      BUNDLE_APP_CONFIG="$GEM_HOME"
  ENV PATH=$GEM_HOME/bin:$PATH
EOF

run = -> truffle_flavor, image_base {
  docker = ""
  if truffle_flavor == "stable"
    docker << "ARG TRUFFLERUBY_VERSION\n\n"
  end

  docker << "RUN set -eux ;\\\n"

  if image_base == "slim"
    # See https://github.com/truffleruby/truffleruby/blob/master/README.md#dependencies
    docker << <<~EOF.indent(4)
      apt-get update ;\\
      apt-get install -y --no-install-recommends \\
              make gcc g++ \\
              ca-certificates \\
              libz-dev \\
              tar \\
              wget \\
      ; \\
      rm -rf /var/lib/apt/lists/* ;\\
    EOF
  end

  case truffle_flavor
  when "stable"
    docker << <<~EOF.indent(4)
      case "$(uname -m)" in \\
        x86_64) arch="amd64" ;; \\
        aarch64) arch="aarch64" ;; \\
      esac; \\
      wget -q https://github.com/truffleruby/truffleruby/releases/download/graal-$TRUFFLERUBY_VERSION/truffleruby-$TRUFFLERUBY_VERSION-linux-$arch.tar.gz ;\\
      tar -xzf truffleruby-$TRUFFLERUBY_VERSION-linux-$arch.tar.gz -C /usr/local --strip-components=1 ;\\
      rm truffleruby-$TRUFFLERUBY_VERSION-linux-$arch.tar.gz ;\\
    EOF
  when "nightly"
    docker << <<~EOF.indent(4)
      case "$(uname -m)" in \\
        x86_64) arch="x64" ;; \\
        aarch64) arch="arm64" ;; \\
      esac; \\
      wget -q https://github.com/ruby/truffleruby-dev-builder/releases/latest/download/truffleruby-head-ubuntu-22.04-$arch.tar.gz ;\\
      tar -xzf truffleruby-head-ubuntu-22.04-$arch.tar.gz -C /usr/local --strip-components=1 ;\\
      rm truffleruby-head-ubuntu-22.04-$arch.tar.gz ;\\
    EOF
  end

  docker << <<~EOF.indent(4)
    /usr/local/lib/truffle/post_install_hook.sh ;\\
    ruby --version ;\\
    gem --version ;\\
    bundle --version ;\\
  EOF
  # adjust permissions of a few directories for running "gem install" as an arbitrary user
  docker << <<~EOF.indent(4)
    mkdir -p "$GEM_HOME" && chmod 777 "$GEM_HOME"
  EOF
}

def write_section(io, string, blank_line: true)
  return if string.nil?
  io.puts string
  io.puts if blank_line
end

["stable", "nightly"].each do |truffle_flavor|
  [nil, "slim"].each do |image_base|
    dockerfile_name = "%s.dockerfile" % [truffle_flavor, image_base].compact.join("-")
    dockerfile_path = File.join(__dir__, dockerfile_name)
    puts "writing to #{dockerfile_path} ..."

    File.open(dockerfile_path, "w") do |dockerfile|
      write_section dockerfile, "FROM #{base_images[image_base]}"
      write_section dockerfile, envs
      write_section dockerfile, run[truffle_flavor, image_base]
      write_section dockerfile, 'CMD [ "irb" ]', blank_line: false
    end
  end
end
